#include "stdafx.h"

CPanicButton::CPanicButton( ) {
	debug( L"CPanicButton::`ctor\n" );
	pImplementation = nullptr;
	lidOpen = false;
	buttonPressed = false;
}

CPanicButton::~CPanicButton( ) {
	debug( L"CPanicButton::`dtor\n" );
}

HRESULT CPanicButton::FinalConstruct( ) {
	debug( L"CPanicButton::FinalConstruct\n" );
	return S_OK;
}

void CPanicButton::FinalRelease( ) {
	debug( L"CPanicButton::FinalRelease\n" );
	if ( pImplementation ) {
		debug( L"CPanicButton::FinalRelease: warning: device object wasn't released\n" );
		if ( pImplementation->IsOpen( ) ) {
			debug( L"CPanicButton::FinalRelease: warning: ... device object wasn't even *closed*\n" );
			pImplementation->Close( );
		}
		delete pImplementation;
		pImplementation = nullptr;
	}
}

STDMETHODIMP CPanicButton::OpenDevice( void ) {
	debug( L"CPanicButton::OpenDevice\n" );

	if ( pImplementation ) {
		if ( pImplementation->IsOpen( ) ) {
			debug( L"CPanicButton::OpenDevice: warning: device already open\n" );
			return S_FALSE;
		}
	} else {
		pImplementation = new BigRedButton( this );
	}

	if ( !pImplementation->Open( ) ) {
		delete pImplementation;
		pImplementation = nullptr;
		return E_FAIL;
	}

	return S_OK;
}

STDMETHODIMP CPanicButton::CloseDevice( void ) {
	debug( L"CPanicButton::CloseDevice\n" );

	if ( !pImplementation ) {
		debug( L"CPanicButton::CloseDevice: warning: device not open\n" );
		return S_FALSE;
	}

	pImplementation->Close( );
	delete pImplementation;
	pImplementation = nullptr;

	return S_OK;
}

STDMETHODIMP CPanicButton::get_RefreshRate( int* pVal ) {
	debug( L"CPanicButton::get_RefreshRate\n" );

	if ( NULL == pVal ) {
		return E_POINTER;
	}

	*pVal = pImplementation->GetRefreshRate( );
	return S_OK;
}

STDMETHODIMP CPanicButton::put_RefreshRate( int val ) {
	debug( L"CPanicButton::put_RefreshRate\n" );

	return pImplementation->SetRefreshRate( val ) ? S_OK : E_FAIL;
}

STDMETHODIMP CPanicButton::get_LidOpen( VARIANT_BOOL* pVal ) {
	debug( L"CPanicButton::get_LidOpen\n" );

	if ( !pVal ) {
		return E_POINTER;
	}

	*pVal = lidOpen ? VARIANT_TRUE : VARIANT_FALSE;
	return S_OK;
}

STDMETHODIMP CPanicButton::get_ButtonPressed( VARIANT_BOOL* pVal ) {
	debug( L"CPanicButton::get_ButtonPressed\n" );

	if ( !pVal ) {
		return E_POINTER;
	}

	*pVal = buttonPressed ? VARIANT_TRUE : VARIANT_FALSE;
	return S_OK;
}

STDMETHODIMP CPanicButton::get_IsOpen( VARIANT_BOOL* pVal ) {
	debug( L"CPanicButton::get_IsOpen\n" );

	if ( NULL == pVal ) {
		return E_POINTER;
	}

	*pVal = pImplementation->IsOpen( ) ? VARIANT_TRUE : VARIANT_FALSE;
	return S_OK;
}

void CPanicButton::BLidStateChange( bool lidOpen_ ) {
	debug( L"CPanicButton::BLidStateChange\n" );

	if ( lidOpen != lidOpen_ ) {
		lidOpen = lidOpen_;
		Fire_LidStatusChange( lidOpen );
	}
}

void CPanicButton::BButtonStateChange( bool buttonPressed_ ) {
	debug( L"CPanicButton::BButtonStateChange\n" );

	if ( buttonPressed != buttonPressed_ ) {
		buttonPressed = buttonPressed_;
		Fire_ButtonStatusChange( buttonPressed );
	}
}
